package com.mijie.homi.listener;

import java.util.EventListener;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mijie.homi.common.domain.mood.Mood;
import com.mijie.homi.common.domain.timeline.TimelineEvent;
import com.mijie.homi.service.mood.MoodBroadcastService;
import com.mijie.homi.service.mood.MoodService;
import com.mijie.homi.service.timeline.TimelineService;
import com.mijie.homi.service.user.FriendService;
import com.mijie.homi.service.user.MoodUserService;
import com.mijie.homi.service.user.UserExtInfoService;
import com.mijie.homi.service.user.UserService;

@Service
public class MoodListener implements EventListener{

	@Autowired
	private FriendService friendService;
	@Autowired
	private UserService userService;
	@Autowired
	private UserExtInfoService extInfoService;
	@Autowired
	private IntegrationHandler integrationHandler;
	@Autowired
	private MoodUserService moodUserService;
	@Autowired
	private MoodService moodService;
	@Autowired
	private MoodBroadcastService moodBroadcastService;
	
	private Logger logger = Logger.getLogger(MoodListener.class);
	
	@Transactional(rollbackFor={Exception.class})
	public void post(Mood mood) {
		extInfoService.settingMood(mood.getUserId(), mood.getId(), mood.getMood());
		
		integrationHandler.postMood(mood.getUserId());
		
		if(mood.getContent()!=null && !mood.getContent().trim().isEmpty())
			integrationHandler.shareMoodStory(mood.getUserId());
		
		
		moodUserService.incrMoodUserAmount(mood.getMood());
		
		moodBroadcastService.addToMoodPushQueue(mood);
		moodService.makeTopicTagForUser(mood.getUserId(),mood.getMood());
	}
	
	public void delete(Mood mood) {
	}
	
	
	public void repost(Mood mood) {
		if(mood.getContent()!=null && !mood.getContent().trim().isEmpty()){
			moodBroadcastService.addToMoodPushQueue(mood);
			integrationHandler.shareMoodStory(mood.getUserId());
		}
	}
	
}
